// pages/search/search.js
const app = getApp()
var utils = require('../../utils/util.js');
function in_array(search, array) {
    for (var i in array) {
        if (array[i]['name'] == search) {
            return true;
        }
    }
    return false;
}
Page({

    /**
     * 页面的初始数据
     */
    data: {
        currentPage:1,
        keyword:"",
        ishide:true,
        searchRecord: []
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        this.setData({
            searchRecord: wx.getStorageSync("recordPost")
        })
        this.goodsList(this.data.currentPage,this.data.keyword);
    },
    //热销排行榜
    goodsList: function (currentPage,keyword) {
        var that = this;
        utils.http("/api/v1/goods/goods_list", {
            page: currentPage,
            keyword: keyword,
			school_id: wx.getStorageSync("schoolInfo").schoolid
        }, function (res) {
            if (res.state == 1) {
                var allpost = that.data.goods;
                if (allpost == undefined) {
                    allpost = [];
                }
                for (var i in res.datas) {
                    allpost.push(res.datas[i])
                }
                if (res.datas.length < 10) {
                    that.setData({
                        tishi_title: '已加载全部',
                    });
                } else {
                    that.setData({
                        tishi_title: '加载更多',
                    });
                }
                that.setData({
                    goods: allpost,
                    ready: res.datas.length == 0 ? 1 : 2,
                })
            } else {
                utils.message(res.datas)
            }
        })
    },
    //商品筛选获取关键词
    onBindTap: function (e) {
        var that = this;
        if (e.detail.value.length <= 0) {
            that.setData({
                ishide: true,
            })
        }
        that.setData({
            keyword: e.detail.value
        })

    },
    //数据去重
    uniqueSearchRecord: function (newRecord) {
        var that = this;
        var n = []; // 构建新数组
        var post = {
            name: newRecord
        }
        n.push(post);
        var recordPost = that.data.searchRecord || [];
        for (var i = 0; i < recordPost.length; i++) {
            if (n.length < 8) {
                //如果当前数组的第i个已经保存进了临时数组，那么跳过，否者吧当前项push到临时数组里面
                if (!in_array(recordPost[i]['name'], n) && recordPost[i].name !== newRecord && newRecord !== '' && newRecord !== null && newRecord !== undefined) {
                    n.push(recordPost[i]);
                }
            }
        }
        that.setData({
            searchRecord: n
        });
        wx.setStorageSync("recordPost", n);
    },
    // 清除所有搜索记录
    onDelAllTap: function (e) {
        var that = this;
        that.setData({
            searchRecord: []
        });
        wx.removeStorageSync('recordPost');
    },
    //商品筛选条件
    onSearchTap: function (e) {
        var that = this;
        if (that.data.keyword.length != 0) {
            that.uniqueSearchRecord(that.data.keyword)
        }
        that.setData({
            ishide: false,
            currentPage: 1,
            goods: [],
        })
        this.goodsList(this.data.currentPage, this.data.keyword);
    },
    //热门搜索
    onHotSearchTap: function (e) {
        var that = this;
        that.setData({
            keyword: e.currentTarget.dataset.name
        })
    },
    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function () {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function () {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function () {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {
		var that = this;
		if (that.data.ready == 2) {
			var page = that.data.currentPage + 1
			that.setData({
				currentPage: page,
			})
			this.goodsList(this.data.currentPage, this.data.keyword);
		}
    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function () {

    }
})